package cn.jsony.pet.service.impl;

import cn.jsony.basic.service.IBaiduAiAuditService;
import cn.jsony.basic.util.AjaxResult;
import cn.jsony.basic.util.LoginContext;
import cn.jsony.org.domain.Employee;
import cn.jsony.org.mapper.EmployeeMapper;
import cn.jsony.pet.domain.Pet;
import cn.jsony.pet.domain.PetDetail;
import cn.jsony.pet.domain.PetOnlineAuditLog;
import cn.jsony.pet.mapper.PetDetailMapper;
import cn.jsony.pet.mapper.PetMapper;
import cn.jsony.pet.mapper.PetOnlineAuditLogMapper;
import cn.jsony.pet.service.IPetService;
import cn.jsony.basic.service.impl.BaseServiceImpl;
import cn.jsony.user.domain.Logininfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * 业务实现类：
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {

    @Autowired
    private PetDetailMapper petDetailMapper;

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private IBaiduAiAuditService baiduAiAuditService;

    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;


    @Transactional
    @Override
    public void add(Pet pet) {
        super.add(pet);

        PetDetail petDetail = pet.getPetDetail();

        if (petDetail!=null){

            petDetail.setPetId(pet.getId());

            petDetailMapper.save(petDetail);
        }

    }

    @Override
    public void update(Pet pet) {
        super.update(pet);

        PetDetail petDetail = pet.getPetDetail();

        if (petDetail!=null){

            petDetailMapper.update(petDetail);
        }
    }

    @Transactional
    @Override
    public void del(Long id) {
        super.del(id);

        petDetailMapper.delPetDetailByPetId(id);
    }

    @Override
    public AjaxResult onsale(List<Long> ids, HttpServletRequest request) {
        //上架 -不能用批量操作，有上架审核
        for (Long id : ids) {
            //上架自动审核文本
            Pet pet = petMapper.loadById(id);
            String auditText = pet.getName();
            Boolean textBoo = baiduAiAuditService.textAudit(auditText);
            //审核图片：多张图片resources
            String petResources = pet.getResources();
            Boolean imageBoo = true;
            if(!StringUtils.isEmpty(petResources)){
                imageBoo = baiduAiAuditService.imageAudit(petResources);
            }

            //获取审核人
            Logininfo logininfo = (Logininfo) LoginContext.getLogininfo(request);
            Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());

            //审核通过
            if (textBoo && imageBoo){
                //数据库操作
                Map<String,Object> params = new HashMap<>();
                params.put("id",id);
                params.put("onsaletime",new Date());
                petMapper.onsale(params);
                //审核成功-note

                //记录审核日志
                PetOnlineAuditLog auditLog = new PetOnlineAuditLog();
                auditLog.setState(1);
                auditLog.setPetId(id);
                auditLog.setAuditId(employee.getId());
                auditLog.setNote("审核成功！");
                petOnlineAuditLogMapper.save(auditLog);
            }else{
                //审核失败-note-当前是下架状态，如果审核失败！
                //记录审核日志
                PetOnlineAuditLog auditLog = new PetOnlineAuditLog();
                auditLog.setState(0);
                auditLog.setPetId(id);
                auditLog.setAuditId(employee.getId());
                auditLog.setNote("审核失败,宠物名称或图片不合法!!!");
                petOnlineAuditLogMapper.save(auditLog);
                //审核失败，上架失败
                return AjaxResult.me().setSuccess(false).setMsg("上架失败！审核失败,宠物名称或图片不合法!!!");
            }
        }
        return AjaxResult.me();
    }

    @Override
    public AjaxResult offsale(List<Long> ids, HttpServletRequest request) {
        //下架：修改state为0，offsaletime为当前时间 ,可以用批量操作
        Map<String,Object> params = new HashMap<>();
        params.put("ids",ids);
        params.put("offsaletime",new Date());
        petMapper.offsale(params);
        return AjaxResult.me();
    }

    @Override
    public Pet findOnebyshopid(Long id) {

        return petMapper.findOnebyshopid(id);
    }
}
